package cn.lili.controller;


import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.service.UrbanSubstationService;
import cn.lili.modules.urbansubstation.entity.dos.UrbanSubstation;
import cn.lili.modules.urbansubstation.entity.dto.UrbanSubstationDTO;
import cn.lili.modules.urbansubstation.entity.dto.UrbanSubstationEditDTO;
import cn.lili.modules.urbansubstation.entity.dto.UrbanSubstationSearchParams;
import cn.lili.modules.urbansubstation.entity.vos.UrbanSubstationGetVo;
import cn.lili.modules.urbansubstation.entity.vos.UrbanSubstationVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = "城市分站")
@RequestMapping("/urban-substation")
@RequiredArgsConstructor
@Slf4j
public class UrbanSubstationController {

    private final UrbanSubstationService substationService;

    private final UserClient userClient;

    @ApiOperation(value = "添加城市分站")
    @PostMapping(value = "/add")
    public ResultMessage<UrbanSubstation> add(@Valid @RequestBody UrbanSubstationDTO urbanSubstationDTO) {
        return ResultUtil.data(substationService.add(urbanSubstationDTO));
    }

    @ApiOperation(value = "获取城市分站分页列表")
    @GetMapping
    public ResultMessage<Page<UrbanSubstationVo>> getByPage(UrbanSubstationSearchParams searchParams, PageVO page) {
        return ResultUtil.data(substationService.findByConditionPage(searchParams, page));
    }

    @ApiOperation(value = "获取城市分站详情")
    @ApiImplicitParam(name = "id", value = "城市分站id", required = true, paramType = "path", dataType = "String")
    @GetMapping(value = "/{id}")
    public ResultMessage<UrbanSubstation> detail(@PathVariable String id) {
        return ResultUtil.data(substationService.getUrbanSubstationById(id));
    }

    @ApiOperation(value = "编辑城市分站")
    @PostMapping(value = "/edit")
    public ResultMessage<UrbanSubstation> edit(@Valid @RequestBody UrbanSubstationEditDTO urbanSubstationEditDTO) {
        return ResultUtil.data(substationService.edit(urbanSubstationEditDTO));
    }

    @ApiOperation(value = "关闭城市分站")
    @ApiImplicitParam(name = "id", value = "城市分站id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/disable/{id}")
    public ResultMessage<UrbanSubstation> disable(@PathVariable String id) {
        substationService.disable(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "开启城市分站")
    @ApiImplicitParam(name = "id", value = "城市分站id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/enable/{id}")
    public ResultMessage<UrbanSubstation> enable(@PathVariable String id) {
        substationService.enable(id, null);
        return ResultUtil.success();
    }

    @ApiOperation(value = "审核城市分站")
    @ApiImplicitParam(name = "id", value = "城市分站id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/updateStatus/{id}/{status}")
    public ResultMessage<UrbanSubstation> updateStatus(@PathVariable String id, @PathVariable String status) {
        substationService.enable(id, status);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除城市分站")
    @ApiImplicitParam(name = "id", value = "城市分站id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/delete/{id}")
    public ResultMessage<UrbanSubstation> delete(@PathVariable String id) {
        substationService.delete(id);
        return ResultUtil.success();
    }

    /**
     * 城市站点下拉框
     *
     * @param name 站点所属城市名称
     * @return 站点城市集合
     */
    @GetMapping("/select")
    public ResultMessage<Object> select(String name) {
        return ResultUtil.data(substationService.select(name));
    }

    @ApiOperation(value = "获取自己城市分站信息")
    @GetMapping(value = "/owner")
    public ResultMessage<UrbanSubstationGetVo> get() {
        UrbanSubstationGetVo urbanSubstationGetDTO = new UrbanSubstationGetVo();
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        UrbanSubstation urbanSubstation = substationService.getUrbanSubstation(currentUser.getId());
        if (null != urbanSubstation) {
            BeanUtils.copyProperties(urbanSubstation, urbanSubstationGetDTO);
        }
        User user = userClient.getById(currentUser.getId());
        if (user!=null) {
            urbanSubstationGetDTO.setOpenId(user.getOpenId());
            urbanSubstationGetDTO.setAccount(user.getAccount());
            urbanSubstationGetDTO.setFyName(user.getFyName());
        }
        //获取当前登录商家内容
        return ResultUtil.data(urbanSubstationGetDTO);
    }


    @GetMapping("/getOperationCenterUrbanSubstationList")
    @ApiOperation("获取运营中心省份下的所有城市分站")
    public ResultMessage<List<UrbanSubstation>> getOperationCenterUrbanSubstationList() {
        return ResultUtil.data(substationService.getOperationCenterUrbanSubstationList());
    }
}
